package com.Ajava1704.多线程使用案例;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/2/25  15:01
 * 描述:
 */
public class TestMoreThread {
    public static void main(String[] args) throws InterruptedException, ExecutionException {

         Long start=System.currentTimeMillis();

         List<Map<String,Object>> list=new ArrayList<>();

         for(int i=0;i<40000;i++){

             Map<String,Object> map=new HashMap<>();
             map.put("id",i);
             list.add(map);
         }


        /*for (Map<String, Object> map : list) {
            map.put("goodaName",new String(new Random().nextDouble()+""));
            map.put("brandName",new String(new Random().nextDouble()+""));
            map.put("money",new Random().nextDouble());
            map.put("goodsStyle",new String(new Random().nextInt(100000)+""));
            map.put("address",new String(new Random().nextDouble()+"地址信息"));
            map.put("lianXiRen","联系人");
            map.put("Tel",new String(new Random().nextDouble()+"电话"));
        }*/

        FutureTask<List<Map<String,Object>>> futureTask1=new FutureTask<>(new TestHandler(list.subList(0,9999)));
        FutureTask<List<Map<String,Object>>> futureTask2=new FutureTask<>(new TestHandler(list.subList(10000,19999)));
        FutureTask<List<Map<String,Object>>> futureTask3=new FutureTask<>(new TestHandler(list.subList(20000,29999)));
        FutureTask<List<Map<String,Object>>> futureTask4=new FutureTask<>(new TestHandler(list.subList(30000,39999)));

         Thread thread1=new Thread(futureTask1);
         Thread thread2=new Thread(futureTask2);
         Thread thread3=new Thread(futureTask3);
         Thread thread4=new Thread(futureTask4);

            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();

        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();


        List<Map<String,Object>> list2=new ArrayList<>();
        list2.addAll(futureTask1.get());
        list2.addAll(futureTask2.get());
        list2.addAll(futureTask3.get());
        list2.addAll(futureTask4.get());


        Long end=System.currentTimeMillis();
        System.out.println(end-start);
        //System.out.println(list2.size()+"======>"+list2.get(500));







    }
}
